package cn.icare.cms.impl;

import cn.icare.cms.ListUtil;
import cn.icare.cms.StringUtil;
import cn.icare.cms.UnitService;
import cn.icare.cms.dao.mapper.UnitMapper;
import cn.icare.cms.dao.po.Unit;
import cn.icare.cms.model.ro.AddUnitRO;
import cn.icare.cms.model.ro.EditUnitRO;
import cn.icare.cms.model.vo.rest.MsgDatasVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UnitServiceImpl implements UnitService {
    @Resource
    private UnitMapper unitMapper;
    private final Logger logger = LoggerFactory.getLogger(UnitServiceImpl.class);

    @Override
    public Long createOne(AddUnitRO ro) {
        Unit model = new Unit();
        model.setName(ro.getName());
       model.setSortindex(ro.getSortindex());

        int result = this.unitMapper.insert(model);
        if (result > 0) {
            return model.getId();
        }
        return 0L;
    }

    @Override
    public boolean updateOne(EditUnitRO ro) {
        Unit model = new Unit();
        model.setId(ro.getId());
        model.setName(ro.getName());
        model.setSortindex(ro.getSortindex());

        int result = this.unitMapper.updateByPrimaryKeySelective(model);

        return result>0;
    }

    @Override
    public boolean deleteOne(Long id) {
        boolean result = false;
        try {
            result = this.unitMapper.deleteByPrimaryKey(id) > 0;
        } catch (Exception e) {
            this.logger.error("deleteOne 异常", e);
        }
        return result;
    }

    @Override
    public boolean deleteBatch(List<Long> ids) {
        boolean result = false;
        try {
            if (ListUtil.isNotEmpty(ids)) {
                Example example = new Example(Unit.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andIn("id", ids);
                result = this.unitMapper.deleteByExample(example) > 0;
            }
        } catch (Exception e) {
            this.logger.error("deleteBatch 异常", e);
        }
        return result;
    }

    @Override
    public Unit getOne(Long id) {

        return  this.unitMapper.selectByPrimaryKey(id);
    }

    @Override
    public MsgDatasVO getList(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        MsgDatasVO result = new MsgDatasVO();
        try {
            Example example = new Example(Unit.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtil.isNotEmpty(name)) {
                criteria.andLike("name", "%" +name + "%");
            }
            example.setOrderByClause("sortindex desc");
            List<Unit> dataList = this.unitMapper.selectByExample(example);
            PageInfo pageInfo = new PageInfo(dataList);
            result.setTotal(pageInfo.getTotal());
            result.setDatas(dataList);
        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }

    @Override
    public MsgDatasVO getAllList(String name) {
        MsgDatasVO result = new MsgDatasVO();
        try {
            Example example = new Example(Unit.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtil.isNotEmpty(name)) {
                criteria.andLike("name", "%" +name + "%");
            }
            List<Unit> dataList = this.unitMapper.selectByExample(example);
            if(dataList!=null) {
                result.setTotal(dataList.size());
                result.setDatas(dataList);
            }

        } catch (Exception e) {
            this.logger.error("getList 异常", e);
        }
        return result;
    }
}
